//
//  SHPayPasswordView.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2018/1/15.
//  Copyright © 2018年 SH_iOS. All rights reserved.
//
#import "SHPasswordCodeView.h"
#import "SHPayPasswordView.h"
@interface SHPayPasswordView()
@property(nonatomic,strong)UIButton *grayBtn;
@property(nonatomic,strong)UIButton *closseBtn;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *line;
@property(nonatomic,strong)UIButton *forgetPsBtn;

@property(nonatomic,strong)NSString *passwordKey;//支付密码
/**
 *  密码框，用来方便控制其内容是否清空
 */
@property(nonatomic,strong)SHPasswordCodeView *passwordView;

@end
@implementation SHPayPasswordView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
 
        
       [self createSubViews];
        
        __weak SHPayPasswordView *weakSelf = self;
        self.passwordView.EndEditBlcok = ^(NSString *password) {
            
            if (password.length==6) {
                if (_delegate && [_delegate respondsToSelector:@selector(passwordView:passwordKey:)]) {
                    
                    [weakSelf .delegate passwordView:weakSelf passwordKey:password];
//                     [weakself.passwordView clearText];
                }
                
            }
        };
        
    }
    
    return self;
}
- (UIView *)contenView{ 
    
    if (!_contenView) {
        _contenView = [[UIView alloc]initWithFrame:CGRectMake(0,KScreenHeight - __I6Height(450), KScreenWidth, __I6Height(450))];
        _contenView.backgroundColor = [UIColor whiteColor];
       

    }
    return _contenView;
}

- (UIButton *)grayBtn{
    if (!_grayBtn) {
        
        _grayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_grayBtn addTarget:self action:@selector(dissmissMenuView) forControlEvents:UIControlEventTouchUpInside];
        _grayBtn.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
        _grayBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        
    }
    return _grayBtn;
}
- (UIButton *)closseBtn{
    if (!_closseBtn) {
        
        _closseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _closseBtn.frame = CGRectMake(__I6Width(11), __I6Height(11), __I6Width(30), __I6Height(25));
        [_closseBtn addTarget:self action:@selector(dissmissMenuView) forControlEvents:UIControlEventTouchUpInside];
        [_closseBtn setImage:[UIImage imageNamed:@"close_cha"] forState:UIControlStateNormal];
    }
    return _closseBtn;
}



- (UILabel *)titleLabel{
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(__I6Width(80), __I6Height(15),KScreenWidth - __I6Width(160), __I6Height(18))];
        _titleLabel.font = KSystemFont(__I6Width(18));
        _titleLabel.textColor = KSHTEXTCOLOR;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"请输入支付密码";
    }
    return _titleLabel;
}

- (UIView *)line{
    if (!_line) {
        _line = [[UIView alloc]initWithFrame:CGRectMake(0, __I6Height(47), KScreenWidth, .5)];
        _line.backgroundColor = KLINECOLOR;
    }
    return _line;
}

- (SHPasswordCodeView *)passwordView{
    
    if (!_passwordView) {
        
        _passwordView = [[SHPasswordCodeView alloc] initWithFrame:CGRectMake((self.frame.size.width-__I6Width(300))/2,__I6Height(70),__I6Width(300),__I6Height(50)) num:6 lineColor:KLINECOLOR textFont:30];
        //下划线
        _passwordView.hasUnderLine = NO;
        //分割线
        _passwordView.hasSpaceLine = YES;
        
        //输入风格
        _passwordView.codeType = CodeViewTypeSecret;
        
        [_passwordView beginEdit];
       
        
    
    }
    return _passwordView;
}

- (UIButton *)forgetPsBtn{
    if (!_forgetPsBtn) {
        
        _forgetPsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _forgetPsBtn.frame = CGRectMake(KScreenWidth - __I6Width(140), __I6Height(130),  __I6Width(120), __I6Height(41));
        _forgetPsBtn.titleLabel.font = KSHSystemFont(14);
        [_forgetPsBtn setTitleColor:KREDCOLOR forState:UIControlStateNormal];
        _forgetPsBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_forgetPsBtn setTitle:@"忘记密码 ？ " forState:UIControlStateNormal];
        [_forgetPsBtn addTarget:self action:@selector(forgetPayPsAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetPsBtn;
}

- (void)createSubViews{
    
    [self addSubview:self.grayBtn];
    [self.grayBtn addSubview:self.contenView];
    [self.contenView addSubview:self.closseBtn];
    [self.contenView addSubview:self.titleLabel];
    [self.contenView addSubview:self.line];
    [self.contenView addSubview:self.passwordView];
    [self.contenView addSubview:self.forgetPsBtn];
}

#pragma mark - show

- (void)show{

    CGRect frame  = self.contenView.frame;
    frame.origin.y = KScreenHeight - __I6Height(450) ;
    __weak typeof(self) weakself = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakself.grayBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        weakself.contenView.frame = frame;

    }completion:^(BOOL finished) {

    }];
}

#pragma mark - dissmiss

- (void)dissmissMenuView{
    
    CGRect frame  = self.contenView.frame;
    frame.origin.y = KScreenHeight;
    [self.passwordView endEdit];
    [UIView animateWithDuration:0.5 animations:^{
        self.contenView.frame = frame;
        _grayBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}


#pragma mark - 忘记支付密码
- (void)forgetPayPsAction:(UIButton *)sender{
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(userforgetPayPsAction)]) {
        
        [_delegate userforgetPayPsAction];
    }
    

    [self dissmissMenuView];
    
}

- (void)hidePayPasswordViewSubView{
    self.passwordView.hidden = YES;
    self.forgetPsBtn.hidden = YES;
}

- (void)showPayPasswordViewSubView{
    self.passwordView.hidden = NO;
    self.forgetPsBtn.hidden = NO;
}


- (void)clearPassword{
    [self.passwordView beginEdit];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
